package cc.messcat.web.fupin;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.entity.fupin.PreventPoverty;
import cc.messcat.service.fupin.PreventPovertyService;
import cc.messcat.service.system.UserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 防贫预警控制层
 */
@Controller
@RequestMapping("/prevent")
public class PreventPovertyController {
	static Logger log = LoggerFactory.getLogger(PreventPovertyController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private PreventPovertyService preventPovertyService;


	/**
	 * 根据id获取
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object getPreventPoverty(@PathVariable Long id) {
		PreventPoverty preventPoverty = preventPovertyService.retrieveObject(id);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, preventPoverty);
	}

	/**
	 * 获取列表
	 */
	@RequestMapping(value="list",method = RequestMethod.GET)
	@ResponseBody
	public Object listPreventPoverty(PreventPoverty preventPoverty,Integer pageNo, Integer pageSize){
		Pager<PreventPoverty> preventPovertyPager = preventPovertyService.listPreventPoverty(preventPoverty, pageNo, pageSize);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,preventPovertyPager);
	}

	/**
	 * 获取列表
	 */
	@RequestMapping(value="export",method = RequestMethod.GET)
	@ResponseBody
	public Object exportPreventPoverty(PreventPoverty preventPoverty, Integer pageNo, Integer pageSize, HttpServletResponse response){
		try {

			HSSFWorkbook wb = preventPovertyService.exportPreventPoverty( preventPoverty,  pageNo,  pageSize);

			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode("防贫预警信息表", "UTF-8") + ".xls");
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");

			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
			wb.write(bufferedOutputStream);
			bufferedOutputStream.close();

			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_EXPORT_SUCCESS);
		} catch (IOException e) {
			e.printStackTrace();
			return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_EXPORT_FAIL);
		}
	}

}
